import { ref, reactive, onMounted, nextTick } from 'vue'
import { hazardPageApi, hazardRemoveApi } from '@/api/ReviewBusiness/01Project/project'
import { userPageApi } from '@/api/user/user'

import { Result, StatusCode } from '@/utils/request';
import useInstance from '@/hooks/useInstance';

export default function AssignModel() {
  const { global, proxy } = useInstance();

  const searchParams = reactive({
    name: ''
  })
  const tableData = reactive({
    Loading: false,
    userList: [] as any,
    tabList: [
      { title: '全部', id: 1 },
      { title: '由我创建', id: 2 },
      { title: '指派给我', id: 3 },
      { title: '由我指派', id: 4 },
    ],
    list: [] as any,
    tableHeigth: 0,
    search: {
      page: 1,
      size: 10,
      queryType:0,
      taskId:'',
      status:'' as string
    },
    total: 0
  })
  onMounted(() => {
    getTableList()
    getUserList()
    nextTick(() => {
      tableData.tableHeigth = window.innerHeight - 185
    })
  })
  const getUserList = async () => {
    // 获取用户列表 -- > 后期是不是要考虑权限的问题
    const res = await userPageApi({ page: 1, size: 10000 })
    if (res.code === 200) {
      tableData.userList = res.data.records
    }
  }
  const addTab = (title) => {
    getTableList()
  }
  const resetBtn = () => {
    getTableList()
  }
  const addBtn = () => { }
  const serachBtn = () => {
    getTableList()
  }
  const getTableList = async () => {
    // 就是这个地方来操作把 看怎么设计这个接口 查询的接口 
    tableData.Loading = true
    const res = await hazardPageApi({ ...tableData.search, ...searchParams })
    if (res.code === 200) {
      // console.log(res.data);
      tableData.list = res.data.records
      tableData.total = res.data.total
      tableData.Loading = false
    }
  }
  const handleSizeChange = (size) => {
    tableData.search.size = size
    getTableList()

  }
  const handleCurrentChange = (page) => {
    tableData.search.page = page
    getTableList()
  }
  const delBtn = async (row?: any) => {
    let parm = {
      id: row.id
    }
    const confirm = await global.$myconfirm('确定删除该数据吗?')
    if (confirm) {
      //执行删除操作
      let res = await hazardRemoveApi(parm)
      if (res && res.code == StatusCode.Success) {
        //信息提示
        global.$message({ message: '删除成功', type: 'success' })
        //刷新表格
        getTableList();
      }
    } else {
      global.$message({ message: '取消删除', type: 'warning' })
    }
  }
  return {
    searchParams, tableData, addTab, resetBtn, addBtn, serachBtn, handleSizeChange, handleCurrentChange, delBtn,getTableList
  }
}